package com.coalmine.activiti.webadmin.upms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.coalmine.activiti.webadmin.upms.dao.TableMapper;
import com.coalmine.activiti.webadmin.upms.model.Table;
import com.coalmine.activiti.webadmin.upms.service.IActDeBusinessService;
import com.coalmine.common.core.exception.CustomException;

/**
 * @author Liuxiaoyang
 * Date  2020-07-28
 */
@Service
public class TableService {
	
    @Autowired
    TableMapper tableDao;
    /**
     * 默认主键列名
     */
    private static final String DEFAULT_ID_COLUMN_NAME = "id";
    /**
     * 默认流程状态列名
     */
    private static final String DEFAULT_STATUS_COLUMN_NAME = "flow_status";

    public List<Table> getTable(String tableName) {
        return tableDao.getTable(tableName);
    }

    /**
     * 更新流程状态
     *
     * @param tableName
     * @param statusColumnName
     * @param idColumnName
     * @param instId
     * @param status
     * @return
     */
    public int updateTable(String tableName, String statusColumnName, String idColumnName, String instId, int status) {
        idColumnName = StringUtils.isBlank(idColumnName) ? DEFAULT_ID_COLUMN_NAME : idColumnName;
        statusColumnName = StringUtils.isBlank(statusColumnName) ? DEFAULT_STATUS_COLUMN_NAME : statusColumnName;
        return tableDao.updateTable(tableName, statusColumnName, idColumnName, instId, status);
    }

    /**
     * 修改流程状态及更新提交时间
     *
     * @param tableName
     * @param statusColumnName
     * @param idColumnName
     * @param instId
     * @param status
     * @return
     */
    public int updateTableSubmitDate(String tableName, String statusColumnName, String idColumnName, String instId, int status) {
        idColumnName = StringUtils.isBlank(idColumnName) ? DEFAULT_ID_COLUMN_NAME : idColumnName;
        statusColumnName = StringUtils.isBlank(statusColumnName) ? DEFAULT_STATUS_COLUMN_NAME : statusColumnName;
        return tableDao.updateTableSubmitDate(tableName, new Date(), statusColumnName, idColumnName, instId, status);
    }

    /**
     * 根据表名和主键查询数据
     *
     * @param tableName
     * @param idColumnName
     * @param instId
     * @return
     */
    Map getTableByParams(String tableName, String idColumnName, String instId) {
        Map tableMap = tableDao.getTableByParams(tableName, idColumnName, instId);
        if (tableMap == null || tableMap.size() == 0) {
            throw new CustomException("业务查询数为空,表=" + tableName + "idColumnName=" + idColumnName + ",instId=" + instId);
        }
        return tableMap;
    }

}
